package org.standard.notice.action;

import java.io.File;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.apache.commons.fileupload.FileUploadException;
import org.standard.commons.model.Annex;
import org.standard.commons.struts2.BaseAction;
import org.standard.commons.utils.FlipPageInfo;
import org.standard.commons.utils.Utils;
import org.standard.notice.model.Notice;

public class NoticeAction extends BaseAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3116870241707125369L;
	private Notice notice;
	private FlipPageInfo flipPageInfo = new FlipPageInfo();
	
	private File[] annex;
	private String[] annexContentType;
	private String[] annexFileName;
	private List<Annex> annexList;
	
	private String s_theme;
	private String s_content;
	private String s_type;
	

	public String index() {
		
		return CREATE;
	}
	
	public String create() {
		
		notice.setDate(new Date());
		try {
			service.createPOJOAndAnnex(notice, request, annex, annexFileName, annexContentType);
		} catch (FileUploadException e) {
			setERROR(e.getMessage());
			e.printStackTrace();
			return CREATE;
		}
		return search();
	}

	public String search() {
		
		StringBuffer sb = new StringBuffer("from Notice where type is null ");
		if (!Utils.isStringNull(s_theme)) {
			sb.append(" and theme like '%"+s_theme+"%'");
		}
		if (!Utils.isStringNull(s_content)) {
			sb.append(" and content like '%"+s_content+"%'");
		}
		sb.append(" order by date desc");
		flipPageInfo = service.findPOJOList(sb.toString(), flipPageInfo);
		return LIST;
	}
	public String searchRead() {
		
		search();
		return "list_read";
	}
	@SuppressWarnings("unchecked")
	public String modifyPre() {
		
		notice = (Notice) service.findPOJOById(Notice.class, notice.getId());
		annexList = service.findPOJOList("from Annex where tableName = '"+Notice.class.getSimpleName()+"' and tableId = '"+notice.getId()+"'");
		return MODIFY;
	}
	public String detail() {
		
		modifyPre();
		return DETAIL;
	}
	public String detailRead() {
		
		modifyPre();
		return "detail_read";
	}
	public String modify() {
		
		notice.setDate(new Date());
		try {
			service.modifyPOJOAndAnnex(notice, request, annex, annexFileName, annexContentType, notice.getId());
		} catch (FileUploadException e) {
			setERROR(e.getMessage());
			e.printStackTrace();
			return MODIFY;
		} catch (SQLException e) {
			setERROR(e.getMessage());
			e.printStackTrace();
			return MODIFY;
		}
		return search();
	}
	public String delete() {
		
		try {
			service.deletePOJOCascadeAnnex(notice, notice.getId());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return search();
	}
	
	//*****************社情民意部分************************//
	
	public String indexSuggest() {
		
		return "suggest_create";
	}
	public String createSuggest() {
		
		notice.setDate(new Date());
		service.createPOJO(notice);
		return searchSuggest();
	}

	public String searchSuggest() {
		
		StringBuffer sb = new StringBuffer("from Notice where type is not null ");
		if (!Utils.isStringNull(s_type)) {
			sb.append(" and type = '"+s_type+"'");
		}
		if (!Utils.isStringNull(s_theme)) {
			sb.append(" and theme like '%"+s_theme+"%'");
		}
		if (!Utils.isStringNull(s_content)) {
			sb.append(" and content like '%"+s_content+"%'");
		}
		sb.append(" order by date desc");
		flipPageInfo = service.findPOJOList(sb.toString(), flipPageInfo);
		return "suggest_list";
	}
	@SuppressWarnings("unchecked")
	public String modifySuggestPre() {
		
		notice = (Notice) service.findPOJOById(Notice.class, notice.getId());
		return "suggest_modify";
	}
	public String detailSuggest() {
		
		modifySuggestPre();
		return "suggest_detail";
	}
	public String modifySuggest() {
		
		notice.setDate(new Date());
		service.modifyPOJO(notice);
		return searchSuggest();
	}
	public String deleteSuggest() {
		
		service.deletePOJO(notice);
		return searchSuggest();
	}

	public Notice getNotice() {
		return notice;
	}

	public void setNotice(Notice notice) {
		this.notice = notice;
	}

	public FlipPageInfo getFlipPageInfo() {
		return flipPageInfo;
	}

	public void setFlipPageInfo(FlipPageInfo flipPageInfo) {
		this.flipPageInfo = flipPageInfo;
	}

	public String getS_theme() {
		return s_theme;
	}

	public void setS_theme(String s_theme) {
		this.s_theme = s_theme;
	}

	public String getS_content() {
		return s_content;
	}

	public void setS_content(String s_content) {
		this.s_content = s_content;
	}

	public File[] getAnnex() {
		return annex;
	}

	public void setAnnex(File[] annex) {
		this.annex = annex;
	}

	public String[] getAnnexContentType() {
		return annexContentType;
	}

	public void setAnnexContentType(String[] annexContentType) {
		this.annexContentType = annexContentType;
	}

	public String[] getAnnexFileName() {
		return annexFileName;
	}

	public void setAnnexFileName(String[] annexFileName) {
		this.annexFileName = annexFileName;
	}

	public List<Annex> getAnnexList() {
		return annexList;
	}

	public void setAnnexList(List<Annex> annexList) {
		this.annexList = annexList;
	}

	public String getS_type() {
		return s_type;
	}

	public void setS_type(String s_type) {
		this.s_type = s_type;
	}
	
}
